package com.xd.study.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Toast;

import com.xd.study.R;
import com.xd.study.fragment.ViewPagerFragment1;
import com.xd.study.fragment.ViewPagerFragment2;
import com.xd.study.fragment.ViewPagerFragment3;
import com.xd.study.fragment.ViewPagerFragment4;
import com.xd.study.utility.MyFragmentPagerAdapter;
import com.xd.study.utility.MyFragmentStatePagerAdapter;
import com.xd.study.utility.MyPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DGF on 2015/4/28.
 * 使用android.support.v4.app.Fragment，需要获取getSupportFragmentManager()，必须继承FragmentActivity
 * 如果业务逻辑比较简单，推荐使用view+viewpager的组合；
 * 如果业务逻辑比较复杂，推荐使用fragment+viewpager的组合；
 */
public class ViewPagerActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager);

        //通过View对象作为ViewPager的数据源
        List<View> views = new ArrayList<>();
        View view1 = View.inflate(this, R.layout.viewpager_view1, null);
        views.add(view1);
        View view2 = View.inflate(this, R.layout.viewpager_view2, null);
        views.add(view2);
        View view3 = View.inflate(this, R.layout.viewpager_view3, null);
        views.add(view3);
        View view4 = View.inflate(this, R.layout.viewpager_view4, null);
        views.add(view4);

        //通过fragment对象作为ViewPager的数据源
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new ViewPagerFragment1());
        fragments.add(new ViewPagerFragment2());
        fragments.add(new ViewPagerFragment3());
        fragments.add(new ViewPagerFragment4());

        //初始化标题
        List<String> titles = new ArrayList<>();
        titles.add("微信");
        titles.add("通讯录");
        titles.add("发现");
        titles.add("我");

        //设置PagerTabStrip的样式
        PagerTabStrip pagerTabStrip = (PagerTabStrip)this.findViewById(R.id.viewpager_tab);
        pagerTabStrip.setBackgroundColor(Color.LTGRAY);//背景色
        pagerTabStrip.setDrawFullUnderline(false);//标题下方的横线隐藏
        pagerTabStrip.setPadding(10, 10, 10, 10);//标题栏的padding
        pagerTabStrip.setTabIndicatorColor(Color.GREEN);//当前标题下方的线颜色
        pagerTabStrip.setTextColor(Color.BLACK);

        //初始化ViewPager
        ViewPager viewPager = (ViewPager)this.findViewById(R.id.viewpager);

        //view数据源
        MyPagerAdapter pageAdapter = new MyPagerAdapter(views, titles);
        //viewPager.setAdapter(pageAdapter);//view数据源

        //Fragment数据源（不自动销毁）
        MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
                getSupportFragmentManager(), fragments, titles);
        //viewPager.setAdapter(fragmentPagerAdapter);

        //Fragment数据源（自动销毁）
        MyFragmentStatePagerAdapter myFragmentStatePagerAdapter = new MyFragmentStatePagerAdapter(
                getSupportFragmentManager(), fragments, titles
        );
        viewPager.setAdapter(myFragmentStatePagerAdapter);

        //监听器
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {

            }

            @Override
            public void onPageSelected(int i) {
                Toast.makeText(ViewPagerActivity.this, "当前选择了第 "+(i+1)+" 个页面", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }
}
